import process from 'process'
import * as stepWhich from '../../src/tool/which'
import path from 'path'
import {afterEach} from 'mocha'
import {expect} from 'chai'
import sinon from 'sinon'

describe('which tool test case', () => {
    describe('which tool', () => {
        it('if missing completed file, the cache not found', async () => {
            process.env.FLOW_RUNNER_TOOL_CACHE_DIR = path.join(
                __dirname,
                'which'
            )
            const stepToolCache = await stepWhich.which(
                'tool1',
                'v1.0.1',
                'x64'
            )

            expect(stepToolCache).equal('')
        })

        it('if found the cache.complete file, then cache found', async () => {
            process.env.FLOW_RUNNER_TOOL_CACHE_DIR = path.join(
                __dirname,
                'which'
            )
            const stepToolCache = await stepWhich.which(
                'tool1',
                'v1.0.0',
                'x64'
            )

            expect(stepToolCache).equal(
                path.join(__dirname, 'which', 'tool1', 'v1.0.0', 'x64')
            )
        })
    })

    describe('which all tool', () => {
        it('which all tool should return the list of the arch', async () => {
            process.env.FLOW_RUNNER_TOOL_CACHE_DIR = path.join(
                __dirname,
                'which'
            )

            let stepAllVersionToolCache = await stepWhich.whichAllVersions(
                'tool1',
                'x64'
            )

            expect(stepAllVersionToolCache.length).equal(2)
            expect(stepAllVersionToolCache[0]).equal('v1.0.0')
            expect(stepAllVersionToolCache[1]).equal('v1.0.2')

            stepAllVersionToolCache = await stepWhich.whichAllVersions(
                'tool1',
                'arm64'
            )

            expect(stepAllVersionToolCache.length).equal(1)
            expect(stepAllVersionToolCache[0]).equal('v1.0.3')
        })
    })

    afterEach(() => {
        process.env.FLOW_RUNNER_TOOL_CACHE_DIR = ''
    })
})
